import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs'
import { createRouter, createWebHistory } from 'vue-router'

// 路由配置
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
    meta: { 
      title: '首页',
      requiresAuth: true 
    },
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '控制台' }
      },
      {
        path: '/users',
        name: 'UserManagement',
        component: () => import('@/views/user/UserManagement.vue'),
        meta: {
          title: '用户管理',
          requiresAuth: true
        }
      },
      {
        path: '/users/add',
        name: 'UserAdd',
        component: () => import('@/views/user/UserForm.vue'),
        meta: {
          title: '添加用户',
          requiresAuth: true
        }
      },
      {
        path: '/users/edit/:id',
        name: 'UserEdit',
        component: () => import('@/views/user/UserForm.vue'),
        meta: {
          title: '编辑用户',
          requiresAuth: true
        },
        props: true
      },
      {
        path: '/clubs',
        name: 'ClubManagement',
        component: () => import('@/views/club/ClubManagement.vue'),
        meta: {
          title: '社团管理',
          requiresAuth: true
        }
      },
      {
        path: '/clubs/add',
        name: 'ClubAdd',
        component: () => import('@/views/club/ClubForm.vue'),
        meta: {
          title: '添加社团',
          requiresAuth: true
        }
      },
      {
        path: '/clubs/edit/:id',
        name: 'ClubEdit',
        component: () => import('@/views/club/ClubForm.vue'),
        meta: {
          title: '编辑社团',
          requiresAuth: true
        },
        props: true
      },
      {
        path: '/club-members',
        name: 'ClubMemberManagement',
        component: () => import('@/views/clubMember/ClubMemberManagement.vue'),
        meta: {
          title: '社团成员管理',
          requiresAuth: true
        }
      },
      {
        path: '/club-members/add',
        name: 'ClubMemberAdd',
        component: () => import('@/views/clubMember/ClubMemberForm.vue'),
        meta: {
          title: '添加社团成员',
          requiresAuth: true
        }
      },
      {
        path: '/club-members/edit/:id',
        name: 'ClubMemberEdit',
        component: () => import('@/views/clubMember/ClubMemberForm.vue'),
        meta: {
          title: '编辑社团成员',
          requiresAuth: true
        },
        props: true
      },
      {
        path: '/activities',
        name: 'ActivityManagement',
        component: () => import('@/views/activity/ActivityManagement.vue'),
        meta: {
          title: '活动管理',
          requiresAuth: true
        }
      },
      {
        path: '/activities/add',
        name: 'ActivityAdd',
        component: () => import('@/views/activity/ActivityForm.vue'),
        meta: {
          title: '添加活动',
          requiresAuth: true
        }
      },
      {
        path: '/activities/edit/:id',
        name: 'ActivityEdit',
        component: () => import('@/views/activity/ActivityForm.vue'),
        meta: {
          title: '编辑活动',
          requiresAuth: true
        },
        props: true
      },
      {
        path: '/activity-participations',
        name: 'ActivityParticipationManagement',
        component: () => import('@/views/activityParticipation/ActivityParticipationManagement.vue'),
        meta: {
          title: '活动参与管理',
          requiresAuth: true
        }
      },
      {
        path: '/activity-participations/add',
        name: 'ActivityParticipationAdd',
        component: () => import('@/views/activityParticipation/ActivityParticipationForm.vue'),
        meta: {
          title: '添加活动参与记录',
          requiresAuth: true
        }
      },
      {
        path: '/activity-participations/edit/:id',
        name: 'ActivityParticipationEdit',
        component: () => import('@/views/activityParticipation/ActivityParticipationForm.vue'),
        meta: {
          title: '编辑活动参与记录',
          requiresAuth: true
        },
        props: true
      },
      {
        path:'/aichat',
        name:"Chat",
        component:()=>import("@/views/activity/Chat.vue"),
         meta: {
          title: 'AI分析',
         
        }
      },
      {
        path: '/announcements',
        name: 'AnnouncementManagement',
        component: () => import('@/views/announcement/AnnouncementManagement.vue'),
        meta: {
          title: '公告管理',
          requiresAuth: true
        }
      },
      {
        path: '/announcements/add',
        name: 'AnnouncementAdd',
        component: () => import('@/views/announcement/AnnouncementForm.vue'),
        meta: {
          title: '添加公告',
          requiresAuth: true
        }
      },
      {
        path: '/announcements/edit/:id',
        name: 'AnnouncementEdit',
        component: () => import('@/views/announcement/AnnouncementForm.vue'),
        meta: {
          title: '编辑公告',
          requiresAuth: true
        },
        props: true
      },
      {
        path: '/media',
        name: 'MediaManagement',
        component: () => import('@/views/media/MediaManagement.vue'),
        meta: {
          title: '媒体管理',
          requiresAuth: true
        }
      },
      {
        path: '/media/add',
        name: 'MediaAdd',
        component: () => import('@/views/media/MediaForm.vue'),
        meta: {
          title: '添加媒体',
          requiresAuth: true
        }
      },
      {
        path: '/media/edit/:id',
        name: 'MediaEdit',
        component: () => import('@/views/media/MediaForm.vue'),
        meta: {
          title: '编辑媒体',
          requiresAuth: true
        },
        props: true
      },
      {
        path: '/profile',
        name: 'Profile',
        component: () => import('@/views/user/Profile.vue'),
        meta: {
          title: '个人中心',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: { title: '页面未找到' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title || '高校社团信息管理系统'
  
  // 判断该路由是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 获取token
  
    const token = localStorage.getItem('token')
    if (!token) {
      // 未登录，跳转到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 将要跳转的路由path作为参数
      })
    } else {
      next()
    }
  } else {
    // 如果用户已经退出登录，且进入的是登录页，直接放行
    if (to.path === '/login') {
      next()
      return
    }
    next()
  }
})

export default router 